﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using XLZF.MicroServices.IRepository;
using XLZF.MicroServices.Models;
using XLZF.MicroServices.ViewModels;

namespace XLZF.MicroServices.ClothingDictService.Controllers;

/// <summary>
/// Detail
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class ClothingClassDetailController : ControllerBase
{
    private readonly IClothingClassDetailRepository _Repository;

    private readonly IMapper _mapper;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="Repository"></param>
    /// <param name="mapper"></param>
    public ClothingClassDetailController(IClothingClassDetailRepository Repository, IMapper mapper)
    {
        _Repository = Repository;

        _mapper = mapper;
    }

    /// <summary>
    /// 查询
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [Route("[action]")]
    public ActionResult Get()
    {
        List<Clothing_Class_Detail> list = _Repository.GetLists();

        var values = _mapper.Map<List<ClothingDictViewModel>>(list);

        return Ok(values);
    }

    /// <summary>
    /// GetSearch
    /// </summary>
    /// <param name="Seachval"></param>
    /// <param name="pageIndex"></param>
    /// <param name="PageSize"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("[action]")]
    public ActionResult GetSearch(string? Seachval = "", int pageIndex = 1, int PageSize = 10)
    {
        Page<Clothing_Class_Detail>? lis = _Repository.GetPageData(Seachval, false, pageIndex, PageSize);

        var values = _mapper.Map<Page<Clothing_Class_Detail>, Page<ClothingDetailViewModel>>(lis);

        return Ok(values);
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="viewModel"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("[action]")]
    public ActionResult Add(ClothingDetailViewModel viewModel)
    {
        Clothing_Class_Detail model = new Clothing_Class_Detail
        {
            ID = Guid.NewGuid().ToString(),

            DetailName = viewModel.ObjName,

            PID = viewModel.ObjPID,

            Status = 1,

            CreateBy = "1",

            CreateDate = DateTime.Now
        };

        return Ok(_Repository.Add(model));
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="viewModel"></param>
    /// <returns></returns>
    [HttpPut]
    [Route("[action]")]
    public ActionResult Update(ClothingDetailViewModel viewModel)
    {
        Clothing_Class_Detail model = _Repository.Get(viewModel.ObjID);

        if (model != null)
        {
            model.DetailName = viewModel.ObjName;

            model.PID = viewModel.ObjPID;

            model.UpdateBy = "1";

            model.UpdateDate = DateTime.Now;

            return Ok(_Repository.Edit(model));
        }
        else
        {
            return BadRequest();
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="ID"></param>
    /// <returns></returns>
    [HttpDelete]
    [Route("[action]")]
    public ActionResult Delete(string ID)
    {
        var obj = _Repository.Get(ID);

        return Ok(_Repository.Del(obj));
    }
}
